
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'EBAS')
	DROP DATABASE [EBAS] 
GO

CREATE DATABASE [EBAS] COLLATE Danish_Norwegian_CI_AS
GO

use [EBAS]
GO

if not exists (select * from master.dbo.syslogins where loginname = N'EBAS')
BEGIN
	declare @logindb nvarchar(132), @loginlang nvarchar(132) select @logindb = N'EBAS', @loginlang = N'us_english'
	if @logindb is null or not exists (select * from master.dbo.sysdatabases where name = @logindb)
		select @logindb = N'master'
	if @loginlang is null or (not exists (select * from master.dbo.syslanguages where name = @loginlang) and @loginlang <> N'us_english')
		select @loginlang = @@language
	exec sp_addlogin N'EBAS', null, @logindb, @loginlang
END
GO

exec sp_addsrvrolemember N'BUILTIN\Administratorer', sysadmin
GO

if not exists (select * from dbo.sysusers where name = N'EBAS' and uid < 16382)
	EXEC sp_grantdbaccess N'EBAS', N'EBAS'
GO

exec sp_addrolemember N'db_datareader', N'EBAS'
GO

exec sp_addrolemember N'db_datawriter', N'EBAS'
GO

if not exists (select 1 from sys.database_principals where name='role_ebas')
BEGIN
	CREATE ROLE role_ebas AUTHORIZATION EBAS;
END
GO

sp_addrolemember 'role_ebas', 'EBAS'
GO

GRANT EXECUTE TO role_ebas
GO
